/*
 * Copyright (c) 1994-2017, Joyoung Stock CO.LTD.
 * The FWJR Project.
 * All Rights Reserved.
 */

package com.jz.jd.rest.business;

import android.text.TextUtils;

import com.jz.jd.rest.constant.JDConstant;
import com.jz.jd.rest.exception.BusinessException;
import com.jz.jd.rest.response.BannerResponse;
import com.jz.jd.rest.response.BaseResponse;
import com.jz.jd.rest.response.FactoryResponse;
import com.jz.jd.rest.response.HotGoodsResponse;
import com.jz.jd.rest.response.OrderListResponse;
import com.jz.jd.rest.response.OrderResponse;
import com.jz.jd.rest.response.ShoppingCarListResponse;
import com.jz.jd.rest.response.StyleResponse;
import com.jz.jd.rest.response.VIPBalanceResponse;
import com.jz.jd.rest.response.VipGradeResponse;
import com.jz.jd.rest.response.VipInfoResponse;
import com.jz.jd.rest.client.RestClient;
import com.jz.jd.rest.response.AddressResponse;
import com.jz.jd.rest.response.AreaResponse;
import com.jz.jd.rest.response.BankCardResponse;
import com.jz.jd.rest.response.BankResponse;
import com.jz.jd.rest.response.ConfigResponse;
import com.jz.jd.rest.response.EarningResponse;
import com.jz.jd.rest.response.ExpressPriceResponse;
import com.jz.jd.rest.response.ExpressResponse;
import com.jz.jd.rest.response.GoodsDetailResponse;
import com.jz.jd.rest.response.GoodsTabResponse;
import com.jz.jd.rest.response.KeFuResponse;
import com.jz.jd.rest.response.LoginResponse;
import com.jz.jd.rest.response.MemberResponse;
import com.jz.jd.rest.response.MessageNotifyResponse;
import com.jz.jd.rest.response.RedResponse;
import com.jz.jd.rest.response.ShouyiResponse;
import com.jz.jd.rest.response.VIPDetailResponse;
import com.jz.jd.rest.response.WXTokenResponse;
import com.jz.jd.rest.response.WeChatPayResponse;
import com.jz.jd.rest.util.MD5Coder;

import jz.joyoung.robot.util.LogUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Locale;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.RequestBody;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;


public class RestProxy {
    private static final String TAG = "jd-" + RestProxy.class.getSimpleName();
    public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");

    private IRestAPI mRestApi;

    private String mPhoneNum, mPassword;

    private static class SingletonHolder {
        private static final RestProxy INSTANCE = new RestProxy();
    }

    /**
     * 获取单例
     */
    public static RestProxy getInstance() {
        return SingletonHolder.INSTANCE;
    }

    private RestProxy() {
        OkHttpClient okHttpClient = RestClient.getInstance().getOkHttpClient();

        Retrofit retrofitRms = new Retrofit.Builder()
                .client(okHttpClient)
                .baseUrl(JDConstant.HOST)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();

        mRestApi = retrofitRms.create(IRestAPI.class);
    }


    /**
     * 显式时间 00:00
     *
     * @param hour 小时数
     * @param min  分钟数
     * @return 时间 00:00
     */
    public String formatTime(int hour, int min) {
        return String.format(Locale.getDefault(), "%02d:%02d", hour, min);
    }

    /**
     * 生成签名
     */
    private String getSign(String str, byte[] data, String salt) {
        if (TextUtils.isEmpty(str)) {
            LogUtil.w(TAG, "Empty param!");
            return null;
        }

        byte[] strBytes;
        byte[] saltBytes;
        try {
            saltBytes = salt.getBytes("UTF-8");
            strBytes = str.getBytes("UTF-8");
        } catch (UnsupportedEncodingException | NullPointerException e) {
            e.printStackTrace();
            return null;
        }

        int strLength = strBytes.length;
        int dataLength = data == null ? 0 : data.length;
        int saltLength = saltBytes.length;
        byte[] valuesBytes = new byte[strLength + dataLength + saltLength];
        System.arraycopy(strBytes, 0, valuesBytes, 0, strLength);
        if (dataLength > 0) {
            System.arraycopy(data, 0, valuesBytes, strLength, dataLength);
        }
        System.arraycopy(saltBytes, 0, valuesBytes, strLength + dataLength, saltLength);
        MD5Coder md5Coder = new MD5Coder();
        return md5Coder.encrypt2Str(valuesBytes);
    }


    public void getBanner(Observer<BannerResponse> subscriber) {
//        try {
        JSONObject paramObj = new JSONObject();
        String paramJsonStr = paramObj.toString();
        // 注意：这个接口不能进行URLEncoder，否则会异常（因为参数是放到表单里面的，不是QueryString）
        LogUtil.d(TAG, "param => " + paramJsonStr);

        RequestBody requestBody = RequestBody.create(JSON, paramJsonStr);

        mRestApi.getBanner(requestBody)
                .map(new Function<BannerResponse, BannerResponse>() {
                    @Override
                    public BannerResponse apply(BannerResponse response) throws Exception {
                        LogUtil.d(TAG, "Response => " + response);
                        if (!response.isSuccessful()) {
                            LogUtil.d(TAG, "Response => " + response.message);
                            throw new BusinessException(response.code, response.message);
                        }
                        return response;
                    }
                })
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
//        } catch (JSONException e) {
//            subscriber.onError(e);
//            LogUtil.w(TAG, e.getMessage(), e);
//        }
    }


    public void getHotGoods(int page, int pageSize, int descOrAsc, int sortByType, int goodsCategoryId, String goods_type_id, Observer<HotGoodsResponse> subscriber) {

        RequestBody requestBody = new FormBody.Builder().add("page", page + "")
                .add("pagesize", pageSize + "")
                .add("descOrAsc", descOrAsc + "")
                .add("sortByType", sortByType + "")
                .add("goodsCategoryId", goodsCategoryId + "")
                .add("goodsTab.goods_type_ids", goods_type_id)
                .build();

        mRestApi.getHotGoods(requestBody)
                .map(new Function<HotGoodsResponse, HotGoodsResponse>() {
                    @Override
                    public HotGoodsResponse apply(HotGoodsResponse response) throws Exception {
                        LogUtil.d(TAG, "Response => " + response);
                        if (!response.isSuccessful()) {
                            LogUtil.d(TAG, "Response => " + response.message);
                            throw new BusinessException(response.code, response.message);
                        }
                        return response;
                    }
                })
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
//        } catch (JSONException e) {
//            subscriber.onError(e);
//            LogUtil.w(TAG, e.getMessage(), e);
//        }
    }


    public void getRedGoods(String token, Observer<RedResponse> subscriber) {
        FormBody.Builder requestBody = new FormBody.Builder();

        if (token != null && !token.equals("")) {
            requestBody.add("token", token);
        }

        mRestApi.getRedGoods(requestBody.build())
                .map(new Function<RedResponse, RedResponse>() {
                    @Override
                    public RedResponse apply(RedResponse response) throws Exception {
                        LogUtil.d(TAG, "Response => " + response);

                        if (!response.isSuccessful()) {
                            LogUtil.d(TAG, "Response => " + response.message);
                            throw new BusinessException(response.code, response.message);
                        }
                        return response;
                    }
                })
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
//        } catch (JSONException e) {
//            subscriber.onError(e);
//            LogUtil.w(TAG, e.getMessage(), e);
//        }
    }

    public void getGoodsDetail(String token, String good_id, Observer<GoodsDetailResponse> subscriber) {
//        try {
//            JSONObject paramObj = new JSONObject();
//            paramObj.put("goodsTab.goods_id", Integer.valueOf(good_id));
//            String paramJsonStr = paramObj.toString();
        // 注意：这个接口不能进行URLEncoder，否则会异常（因为参数是放到表单里面的，不是QueryString）
//            LogUtil.d(TAG, "param => " + paramJsonStr);

//        RequestBody requestBody = RequestBody.create(JSON, paramJsonStr);
        FormBody.Builder requestBody = new FormBody.Builder().add("goodsTab.goods_id", good_id);

        if (token != null && !token.equals("")) {
            requestBody.add("token", token);
        }

        mRestApi.getGoodsDetail(requestBody.build())
                .map(new Function<GoodsDetailResponse, GoodsDetailResponse>() {
                    @Override
                    public GoodsDetailResponse apply(GoodsDetailResponse response) throws Exception {
                        LogUtil.d(TAG, "Response => " + response);

                        if (!response.isSuccessful()) {
                            LogUtil.d(TAG, "Response => " + response.message);
                            throw new BusinessException(response.code, response.message);
                        }
                        return response;
                    }
                })
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
//        } catch (JSONException e) {
//            subscriber.onError(e);
//            LogUtil.w(TAG, e.getMessage(), e);
//        }
    }

    public void getShoppingCarList(String token, String page, String pageSize, Observer<ShoppingCarListResponse> subscriber) {
        try {
            JSONObject paramObj = new JSONObject();
            paramObj.put("token", token);
            paramObj.put("page", page);
            paramObj.put("pagesize", pageSize);
            String paramJsonStr = paramObj.toString();
            // 注意：这个接口不能进行URLEncoder，否则会异常（因为参数是放到表单里面的，不是QueryString）
            LogUtil.d(TAG, "param => " + paramJsonStr);

//            RequestBody requestBody = RequestBody.create(JSON, paramJsonStr);

            RequestBody requestBody = new FormBody.Builder().add("token", token).add("page", page).add("pageSize", pageSize).build();

            mRestApi.getShoppingCarList(requestBody)
                    .map(new Function<ShoppingCarListResponse, ShoppingCarListResponse>() {
                        @Override
                        public ShoppingCarListResponse apply(ShoppingCarListResponse response) throws Exception {
                            LogUtil.d(TAG, "Response => " + response);

                            if (!response.isSuccessful()) {
                                LogUtil.d(TAG, "Response => " + response.message);
                                throw new BusinessException(response.code, response.message);
                            }
                            return response;
                        }
                    })
                    .subscribeOn(Schedulers.io())
                    .unsubscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(subscriber);
        } catch (JSONException e) {
            subscriber.onError(e);
            LogUtil.w(TAG, e.getMessage(), e);
        }
    }


    public void getAllSize(String goodsid, Observer<StyleResponse> subscriber) {

        RequestBody requestBody = new FormBody.Builder().add("goodsStyleTab.goods_id", goodsid).build();

        mRestApi.getAllSize(requestBody)
                .map(new Function<StyleResponse, StyleResponse>() {
                    @Override
                    public StyleResponse apply(StyleResponse response) throws Exception {
                        LogUtil.d(TAG, "Response => " + response);

                        if (!response.isSuccessful()) {
                            LogUtil.d(TAG, "Response => " + response.message);
                            throw new BusinessException(response.code, response.message);
                        }
                        return response;
                    }
                })
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }

    public void queryGoodsId(String token, String goodsid, String style_size, Observer<StyleResponse> subscriber) {

        FormBody.Builder requestBody = new FormBody.Builder()
                .add("goodsStyleTab.goods_id", goodsid)
                .add("goodsStyleTab.style_size", style_size);

        if (token != null && !token.equals("")) {
            requestBody.add("token", token);
        }


        mRestApi.queryGoodsId(requestBody.build())
                .map(new Function<StyleResponse, StyleResponse>() {
                    @Override
                    public StyleResponse apply(StyleResponse response) throws Exception {
                        LogUtil.d(TAG, "Response => " + response);

                        if (!response.isSuccessful()) {
                            LogUtil.d(TAG, "Response => " + response.message);
                            throw new BusinessException(response.code, response.message);
                        }
                        return response;
                    }
                })
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }

    public void addShoppingCar(String token, String cart_type, String goods_style_id, String goods_num, Observer<BaseResponse> subscriber) {

        RequestBody requestBody = new FormBody.Builder().add("token", token).add("shoppingCartTab.cart_type", cart_type).add("shoppingCartTab.goods_style_id", goods_style_id).add("shoppingCartTab.goods_num", goods_num).build();

        mRestApi.addShoppingCar(requestBody)
                .map(new Function<BaseResponse, BaseResponse>() {
                    @Override
                    public BaseResponse apply(BaseResponse response) throws Exception {
                        LogUtil.d(TAG, "Response => " + response);

                        if (!response.isSuccessful()) {
                            LogUtil.d(TAG, "Response => " + response.message);
                            throw new BusinessException(response.code, response.message);
                        }
                        return response;
                    }
                })
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }

    public void getAllOrderList(String token, String order_state, Observer<OrderListResponse> subscriber) {

        RequestBody requestBody = new FormBody.Builder().add("token", token).add("orderTab.order_state", order_state).build();

        mRestApi.getAllOrderList(requestBody)
                .map(new Function<OrderListResponse, OrderListResponse>() {
                    @Override
                    public OrderListResponse apply(OrderListResponse response) throws Exception {
                        LogUtil.d(TAG, "Response => " + response);

                        if (!response.isSuccessful()) {
                            LogUtil.d(TAG, "Response => " + response.message);
                            throw new BusinessException(response.code, response.message);
                        }
                        return response;
                    }
                })
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }


    public void doOrder(String token, String consignee_phone, String consignee_name, String shipping_address, String pay_type, String logistics_type, List<ShoppingCarListResponse.ShoppingCar> list, Observer<OrderResponse> subscriber) {

        try {
            JSONArray array = new JSONArray();
            JSONObject jsonObject = null;
            for (int i = 0; i < list.size(); i++) {
                jsonObject = new JSONObject();
                jsonObject.put("num", list.get(i).goods_num);
                jsonObject.put("goodsStyleId", list.get(i).goods_style_id);
                jsonObject.put("shopping_cart_id", list.get(i).shopping_cart_id);
                array.put(jsonObject);
            }
            RequestBody requestBody = new FormBody.Builder().add("token", token)
                    .add("orderTab.consignee_phone", consignee_phone)
                    .add("orderTab.consignee_name", consignee_name)
                    .add("orderTab.shipping_address", shipping_address)
                    .add("orderTab.pay_type", pay_type)
                    .add("orderTab.orderStyleIdNumJson", array.toString())
                    .add("orderTab.logistics_type", logistics_type)
                    .build();

            mRestApi.doOrder(requestBody)
                    .map(new Function<OrderResponse, OrderResponse>() {
                        @Override
                        public OrderResponse apply(OrderResponse response) throws Exception {
                            LogUtil.d(TAG, "Response => " + response);

                            if (!response.isSuccessful()) {
                                LogUtil.d(TAG, "Response => " + response.message);
                                throw new BusinessException(response.code, response.message);
                            }
                            return response;
                        }
                    })
                    .subscribeOn(Schedulers.io())
                    .unsubscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(subscriber);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }


    public void getDefaultAddress(String token, Observer<AddressResponse> subscriber) {

        RequestBody requestBody = new FormBody.Builder().add("token", token).build();

        mRestApi.getDefaultAddress(requestBody)
                .map(new Function<AddressResponse, AddressResponse>() {
                    @Override
                    public AddressResponse apply(AddressResponse response) throws Exception {
                        LogUtil.d(TAG, "Response => " + response);

                        if (!response.isSuccessful()) {
                            LogUtil.d(TAG, "Response => " + response.message);
                            throw new BusinessException(response.code, response.message);
                        }
                        return response;
                    }
                })
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }

    public void getAllAddress(String token, Observer<AddressResponse> subscriber) {

        RequestBody requestBody = new FormBody.Builder().add("token", token).build();

        mRestApi.getAllAddress(requestBody)
                .map(new Function<AddressResponse, AddressResponse>() {
                    @Override
                    public AddressResponse apply(AddressResponse response) throws Exception {
                        LogUtil.d(TAG, "Response => " + response);

                        if (!response.isSuccessful()) {
                            LogUtil.d(TAG, "Response => " + response.message);
                            throw new BusinessException(response.code, response.message);
                        }
                        return response;
                    }
                })
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }


    public void setDefaultAddress(String token, String user_address_id, Observer<BaseResponse> subscriber) {

        RequestBody requestBody = new FormBody.Builder().add("token", token)
                .add("userAddressTab.user_address_id", user_address_id)
                .build();

        mRestApi.setDefaultAddress(requestBody)
                .map(new Function<BaseResponse, BaseResponse>() {
                    @Override
                    public BaseResponse apply(BaseResponse response) throws Exception {
                        LogUtil.d(TAG, "Response => " + response);

                        if (!response.isSuccessful()) {
                            LogUtil.d(TAG, "Response => " + response.message);
                            throw new BusinessException(response.code, response.message);
                        }
                        return response;
                    }
                })
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }


    public void confirmOrder(String token, String order_id, Observer<BaseResponse> subscriber) {

        RequestBody requestBody = new FormBody.Builder().add("token", token)
                .add("orderTab.order_id", order_id)
                .build();

        mRestApi.confirmOrder(requestBody)
                .map(new Function<BaseResponse, BaseResponse>() {
                    @Override
                    public BaseResponse apply(BaseResponse response) throws Exception {
                        LogUtil.d(TAG, "Response => " + response);

                        if (!response.isSuccessful()) {
                            LogUtil.d(TAG, "Response => " + response.message);
                            throw new BusinessException(response.code, response.message);
                        }
                        return response;
                    }
                })
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }


    public void getExpressInfo(String token, String order_id, Observer<ExpressResponse> subscriber) {

        RequestBody requestBody = new FormBody.Builder().add("token", token)
                .add("orderLogisticsTab.order_id", order_id)
                .build();

        mRestApi.getExpressInfo(requestBody)
                .map(new Function<ExpressResponse, ExpressResponse>() {
                    @Override
                    public ExpressResponse apply(ExpressResponse response) throws Exception {
                        LogUtil.d(TAG, "Response => " + response);

                        if (!response.isSuccessful()) {
                            LogUtil.d(TAG, "Response => " + response.message);
                            throw new BusinessException(response.code, response.message);
                        }
                        return response;
                    }
                })
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }


    public void getProvince(String token, Observer<AreaResponse> subscriber) {

        RequestBody requestBody = new FormBody.Builder()
                .add("token", token)
                .build();

        mRestApi.getProvince(requestBody)
                .map(new Function<AreaResponse, AreaResponse>() {
                    @Override
                    public AreaResponse apply(AreaResponse response) throws Exception {
                        LogUtil.d(TAG, "Response => " + response);

                        if (!response.isSuccessful()) {
                            LogUtil.d(TAG, "Response => " + response.message);
                            throw new BusinessException(response.code, response.message);
                        }
                        return response;
                    }
                })
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }

    public void getArea(String address_id, Observer<AreaResponse> subscriber) {

        RequestBody requestBody = new FormBody.Builder()
                .add("addressTab.address_id", address_id)
                .build();


        mRestApi.getArea(requestBody)
                .map(new Function<AreaResponse, AreaResponse>() {
                    @Override
                    public AreaResponse apply(AreaResponse response) throws Exception {
                        LogUtil.d(TAG, "Response => " + response);

                        if (!response.isSuccessful()) {
                            LogUtil.d(TAG, "Response => " + response.message);
                            throw new BusinessException(response.code, response.message);
                        }
                        return response;
                    }
                })
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }


    public void getCity(String address_id, Observer<AreaResponse> subscriber) {

        RequestBody requestBody = new FormBody.Builder()
                .add("addressTab.address_id", address_id)
                .build();

        mRestApi.getCity(requestBody)
                .map(new Function<AreaResponse, AreaResponse>() {
                    @Override
                    public AreaResponse apply(AreaResponse response) throws Exception {
                        LogUtil.d(TAG, "Response => " + response);

                        if (!response.isSuccessful()) {
                            LogUtil.d(TAG, "Response => " + response.message);
                            throw new BusinessException(response.code, response.message);
                        }
                        return response;
                    }
                })
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }

    public void addAddress(String token, String address_ids, String address_detail, String phone, String nick_name, Observer<BaseResponse> subscriber) {

        RequestBody requestBody = new FormBody.Builder().add("token", token)
                .add("userAddressTab.address_ids", address_ids)
                .add("userAddressTab.address_detail", address_detail)
                .add("userAddressTab.phone", phone)
                .add("userAddressTab.nick_name", nick_name)
                .build();

        mRestApi.addAddress(requestBody)
                .map(new Function<BaseResponse, BaseResponse>() {
                    @Override
                    public BaseResponse apply(BaseResponse response) throws Exception {
                        LogUtil.d(TAG, "Response => " + response);

                        if (!response.isSuccessful()) {
                            LogUtil.d(TAG, "Response => " + response.message);
                            throw new BusinessException(response.code, response.message);
                        }
                        return response;
                    }
                })
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }


    public void update(String token, String user_address_id, String address_ids, String address_detail, String phone, String nick_name, Observer<BaseResponse> subscriber) {

        RequestBody requestBody = new FormBody.Builder().add("token", token)
                .add("userAddressTab.user_address_id", user_address_id)
                .add("userAddressTab.address_ids", address_ids)
                .add("userAddressTab.address_detail", address_detail)
                .add("userAddressTab.phone", phone)
                .add("userAddressTab.nick_name", nick_name)
                .build();

        mRestApi.updateAddress(requestBody)
                .map(new Function<BaseResponse, BaseResponse>() {
                    @Override
                    public BaseResponse apply(BaseResponse response) throws Exception {
                        LogUtil.d(TAG, "Response => " + response);

                        if (!response.isSuccessful()) {
                            LogUtil.d(TAG, "Response => " + response.message);
                            throw new BusinessException(response.code, response.message);
                        }
                        return response;
                    }
                })
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }

    public void delAddress(String token, String user_address_id, Observer<BaseResponse> subscriber) {

        RequestBody requestBody = new FormBody.Builder().add("token", token)
                .add("userAddressTab.user_address_id", user_address_id)
                .build();

        mRestApi.delAddress(requestBody)
                .map(new Function<BaseResponse, BaseResponse>() {
                    @Override
                    public BaseResponse apply(BaseResponse response) throws Exception {
                        LogUtil.d(TAG, "Response => " + response);

                        if (!response.isSuccessful()) {
                            LogUtil.d(TAG, "Response => " + response.message);
                            throw new BusinessException(response.code, response.message);
                        }
                        return response;
                    }
                })
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }

    public void delShoppingBag(String token, String shopping_cart_id, Observer<BaseResponse> subscriber) {

        RequestBody requestBody = new FormBody.Builder().add("token", token)
                .add("shoppingCartTab.shopping_cart_id", shopping_cart_id)
                .build();

        mRestApi.delShoppingBag(requestBody)
                .map(new Function<BaseResponse, BaseResponse>() {
                    @Override
                    public BaseResponse apply(BaseResponse response) throws Exception {
                        LogUtil.d(TAG, "Response => " + response);

                        if (!response.isSuccessful()) {
                            LogUtil.d(TAG, "Response => " + response.message);
                            throw new BusinessException(response.code, response.message);
                        }
                        return response;
                    }
                })
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }


    public void getYZM(String phone, String captcha_type, Observer<BaseResponse> subscriber) {

        RequestBody requestBody = new FormBody.Builder().add("captcha.phone", phone)
                .add("captcha.captcha_type", captcha_type)
                .build();

        mRestApi.getYZM(requestBody)
                .map(new Function<BaseResponse, BaseResponse>() {
                    @Override
                    public BaseResponse apply(BaseResponse response) throws Exception {
                        LogUtil.d(TAG, "Response => " + response);

                        if (!response.isSuccessful()) {
                            LogUtil.d(TAG, "Response => " + response.message);
                            throw new BusinessException(response.code, response.message);
                        }
                        return response;
                    }
                })
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }


    public void login(String wx_token, String wx_app_openid, String nickname, String head_url, String phone, String gender, String client_type, String captcha, String invitedCode, Observer<LoginResponse> subscriber) {

        RequestBody requestBody = new FormBody.Builder()
                .add("user.wx_token", wx_token)
                .add("user.wx_app_openid", wx_app_openid)
                .add("user.phone", phone)
                .add("user.client_type", client_type)
                .add("user.captcha", captcha)
                .add("invitedCode", invitedCode)
                .add("user.nickname", nickname)
                .add("user.head_url", head_url)
                .add("user.gender", gender)

                .build();

        mRestApi.login(requestBody)
                .map(new Function<LoginResponse, LoginResponse>() {
                    @Override
                    public LoginResponse apply(LoginResponse response) throws Exception {
                        LogUtil.d(TAG, "Response => " + response);

                        if (!response.isSuccessful()) {
                            LogUtil.d(TAG, "Response => " + response.message);
                            throw new BusinessException(response.code, response.message);
                        }
                        return response;
                    }
                })
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }


    public void phoneLogin(String phone, String client_type, String captcha, String invitedCode, Observer<LoginResponse> subscriber) {

        RequestBody requestBody = new FormBody.Builder()
                .add("user.phone", phone)
                .add("user.client_type", client_type)
                .add("user.captcha", captcha)
                .add("invitedCode", invitedCode)
                .build();

        mRestApi.phoneLogin(requestBody)
                .map(new Function<LoginResponse, LoginResponse>() {
                    @Override
                    public LoginResponse apply(LoginResponse response) throws Exception {
                        LogUtil.d(TAG, "Response => " + response);

                        if (!response.isSuccessful()) {
                            LogUtil.d(TAG, "Response => " + response.message);
                            throw new BusinessException(response.code, response.message);
                        }
                        return response;
                    }
                })
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }


    public void getVipInfo(String token, Observer<VipInfoResponse> subscriber) {

        RequestBody requestBody = new FormBody.Builder().add("token", token)
                .build();

        mRestApi.getVIPInfo(requestBody)
                .map(new Function<VipInfoResponse, VipInfoResponse>() {
                    @Override
                    public VipInfoResponse apply(VipInfoResponse response) throws Exception {
                        LogUtil.d(TAG, "Response => " + response);

                        if (!response.isSuccessful()) {
                            LogUtil.d(TAG, "Response => " + response.message);
                            throw new BusinessException(response.code, response.message);
                        }
                        return response;
                    }
                })
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }

    public void uploadFile(String token, File file1, File file2, File file3, String remark, String order_id, Observer<BaseResponse> subscriber) {
        MultipartBody.Builder multipartBody = new MultipartBody.Builder().setType(MultipartBody.MIXED).addFormDataPart("token", token);
        if (file1 != null) {
            multipartBody.addFormDataPart("orderTab.paymentDocumentList[0]", file1.getName(), RequestBody.create(MediaType.parse("image/jpg"), file1));
        }
        if (file2 != null) {
            multipartBody.addFormDataPart("orderTab.paymentDocumentList[1]", file2.getName(), RequestBody.create(MediaType.parse("image/jpg"), file2));
        }
        if (file3 != null) {
            multipartBody.addFormDataPart("orderTab.paymentDocumentList[2]", file3.getName(), RequestBody.create(MediaType.parse("image/jpg"), file3));
        }
        multipartBody.addFormDataPart("orderTab.order_id", order_id);
        multipartBody.addFormDataPart("orderTab.payment_credential_remarks", remark);


        RequestBody requestBody = multipartBody.build();


        mRestApi.uploadFile(requestBody)
                .map(new Function<BaseResponse, BaseResponse>() {
                    @Override
                    public BaseResponse apply(BaseResponse response) throws Exception {
                        LogUtil.d(TAG, "Response => " + response);
                        if (!response.isSuccessful()) {
                            throw new BusinessException(response.code, response.message);
                        }
                        return response;
                    }
                })
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }


    public void uploadVipFile(String token, String vip_credential_remarks, File file1, File file2, File file3, Observer<BaseResponse> subscriber) {
        MultipartBody.Builder multipartBody = new MultipartBody.Builder().setType(MultipartBody.MIXED).addFormDataPart("token", token);
        if (file1 != null) {
            multipartBody.addFormDataPart("user.paymentDocumentList[0]", file1.getName(), RequestBody.create(MediaType.parse("image/jpg"), file1));
        }
        if (file2 != null) {
            multipartBody.addFormDataPart("user.paymentDocumentList[1]", file2.getName(), RequestBody.create(MediaType.parse("image/jpg"), file2));
        }
        if (file3 != null) {
            multipartBody.addFormDataPart("user.paymentDocumentList[2]", file3.getName(), RequestBody.create(MediaType.parse("image/jpg"), file3));
        }
        multipartBody.addFormDataPart("user.vip_credential_remarks", vip_credential_remarks);
//        RequestBody requestBody = new MultipartBody.Builder().setType(MultipartBody.FORM)
//                .addFormDataPart("token",token)
//                .addFormDataPart("user.paymentDocumentList[0]", file1.getName(), RequestBody.create(MediaType.parse("image/*"), file1))
//                .addFormDataPart("user.paymentDocumentList[1]", file2.getName(), RequestBody.create(MediaType.parse("image/*"), file2))
//                .addFormDataPart("user.paymentDocumentList[2]", file3.getName(), RequestBody.create(MediaType.parse("image/*"), file3))
//                .build();
        RequestBody requestBody = multipartBody.build();
        mRestApi.uploadVipFile(requestBody)
                .map(new Function<BaseResponse, BaseResponse>() {
                    @Override
                    public BaseResponse apply(BaseResponse response) throws Exception {
                        LogUtil.d(TAG, "Response => " + response);

                        if (!response.isSuccessful()) {
                            LogUtil.d(TAG, "Response => " + response.message);
                            throw new BusinessException(response.code, response.message);
                        }
                        return response;
                    }
                })
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }


    public void updateShoppingBag(String token, String shopping_cart_id, String goods_num, String goods_style_id, Observer<BaseResponse> subscriber) {

        RequestBody requestBody = new FormBody.Builder().add("token", token)
                .add("shoppingCartTab.shopping_cart_id", shopping_cart_id)
                .add("shoppingCartTab.goods_num", goods_num)
                .add("shoppingCartTab.goods_style_id", goods_style_id)
                .build();

        mRestApi.updateShoppingBag(requestBody)
                .map(new Function<BaseResponse, BaseResponse>() {
                    @Override
                    public BaseResponse apply(BaseResponse response) throws Exception {
                        LogUtil.d(TAG, "Response => " + response);

                        if (!response.isSuccessful()) {
                            LogUtil.d(TAG, "Response => " + response.message);
                            throw new BusinessException(response.code, response.message);
                        }
                        return response;
                    }
                })
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }


    public void getFactory(String token, String sortByType, String descOrAsc, String page, String pagesize, Observer<FactoryResponse> subscriber) {

        RequestBody requestBody = new FormBody.Builder().add("token", token)
                .add("sortByType", sortByType)
                .add("descOrAsc", descOrAsc)
                .add("page", page)
                .add("pagesize", pagesize)
                .build();

        mRestApi.getFactory(requestBody)
                .map(new Function<FactoryResponse, FactoryResponse>() {
                    @Override
                    public FactoryResponse apply(FactoryResponse response) throws Exception {
                        LogUtil.d(TAG, "Response => " + response);

                        if (!response.isSuccessful()) {
                            LogUtil.d(TAG, "Response => " + response.message);
                            throw new BusinessException(response.code, response.message);
                        }
                        return response;
                    }
                })
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }


    public void getVIPDetail(String token, Observer<VIPDetailResponse> subscriber) {

        RequestBody requestBody = new FormBody.Builder().add("token", token)
                .build();

        mRestApi.getVipDetail(requestBody)
                .map(new Function<VIPDetailResponse, VIPDetailResponse>() {
                    @Override
                    public VIPDetailResponse apply(VIPDetailResponse response) throws Exception {
                        LogUtil.d(TAG, "Response => " + response);

                        if (!response.isSuccessful()) {
                            LogUtil.d(TAG, "Response => " + response.message);
                            throw new BusinessException(response.code, response.message);
                        }
                        return response;
                    }
                })
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }

    public void getMessageNotify(String token, String msg_type, Observer<MessageNotifyResponse> subscriber) {

        RequestBody requestBody = new FormBody.Builder().add("token", token)
                .add("messageTab.msg_type", msg_type)
                .build();

        mRestApi.getNotify(requestBody)
                .map(new Function<MessageNotifyResponse, MessageNotifyResponse>() {
                    @Override
                    public MessageNotifyResponse apply(MessageNotifyResponse response) throws Exception {
                        LogUtil.d(TAG, "Response => " + response);

                        if (!response.isSuccessful()) {
                            LogUtil.d(TAG, "Response => " + response.message);
                            throw new BusinessException(response.code, response.message);
                        }
                        return response;
                    }
                })
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }

//    public void getVIPDetail(String token,Observer<VIPDetailResponse> subscriber) {
//
//        RequestBody requestBody = new FormBody.Builder().add("token", token)
//                .build();
//
//        mRestApi.getVipDetail(requestBody)
//                .map(new Function<VIPDetailResponse, VIPDetailResponse>() {
//                    @Override
//                    public VIPDetailResponse apply(VIPDetailResponse response) throws Exception {
//                        LogUtil.d(TAG, "Response => " + response);
//
//                        if (!response.isSuccessful()) {
//                            LogUtil.d(TAG, "Response => " + response.message);
//                            throw new BusinessException(response.code, response.message);
//                        }
//                        return response;
//                    }
//                })
//                .subscribeOn(Schedulers.io())
//                .unsubscribeOn(Schedulers.io())
//                .observeOn(AndroidSchedulers.mainThread())
//                .subscribe(subscriber);
//    }


    public void getHotGoods(int page, int pageSize, String goods_name, Observer<HotGoodsResponse> subscriber) {

        RequestBody requestBody = new FormBody.Builder().add("page", page + "")
                .add("pagesize", pageSize + "")
                .add("goodsTab.goods_name", goods_name)
                .build();

        mRestApi.getHotGoods(requestBody)
                .map(new Function<HotGoodsResponse, HotGoodsResponse>() {
                    @Override
                    public HotGoodsResponse apply(HotGoodsResponse response) throws Exception {
                        LogUtil.d(TAG, "Response => " + response);
                        if (!response.isSuccessful()) {
                            LogUtil.d(TAG, "Response => " + response.message);
                            throw new BusinessException(response.code, response.message);
                        }
                        return response;
                    }
                })
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
//        } catch (JSONException e) {
//            subscriber.onError(e);
//            LogUtil.w(TAG, e.getMessage(), e);
//        }
    }


    public void refund(String token, String order_id, String express_code, String express_name, String remark, Observer<BaseResponse> subscriber) {
        try {
//            JSONArray array = new JSONArray();
            JSONObject jsonObject = null;
            jsonObject = new JSONObject();
            jsonObject.put("express_code", express_code);
            jsonObject.put("express_name", express_name);


//            array.put(jsonObject);


            RequestBody requestBody = new FormBody.Builder().add("token", token)
                    .add("orderTab.order_id", order_id)
                    .add("orderTab.logisticsListJson", jsonObject.toString())
                    .add("orderTab.user_remarks", remark)
                    .build();

            mRestApi.refund(requestBody)
                    .map(new Function<BaseResponse, BaseResponse>() {
                        @Override
                        public BaseResponse apply(BaseResponse response) throws Exception {
                            LogUtil.d(TAG, "Response => " + response);

                            if (!response.isSuccessful()) {
                                LogUtil.d(TAG, "Response => " + response.message);
                                throw new BusinessException(response.code, response.message);
                            }
                            return response;
                        }
                    })
                    .subscribeOn(Schedulers.io())
                    .unsubscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(subscriber);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }


    public void getEarning(String token, String add_time, Observer<EarningResponse> subscriber) {
//            JSONArray array = new JSONArray();

//            array.put(jsonObject);


        RequestBody requestBody = new FormBody.Builder()
                .add("token", token)
                .add("checkoutChildTab.add_time", add_time)
                .build();

        mRestApi.getEarning(requestBody)
                .map(new Function<EarningResponse, EarningResponse>() {
                    @Override
                    public EarningResponse apply(EarningResponse response) throws Exception {
                        LogUtil.d(TAG, "Response => " + response);

                        if (!response.isSuccessful()) {
                            LogUtil.d(TAG, "Response => " + response.message);
                            throw new BusinessException(response.code, response.message);
                        }
                        return response;
                    }
                })
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }


    public void getBankCardInfo(String token, Observer<BankCardResponse> subscriber) {
//            JSONArray array = new JSONArray();

//            array.put(jsonObject);


        RequestBody requestBody = new FormBody.Builder()
                .add("token", token)
                .build();

        mRestApi.getBankCardInfo(requestBody)
                .map(new Function<BankCardResponse, BankCardResponse>() {
                    @Override
                    public BankCardResponse apply(BankCardResponse response) throws Exception {
                        LogUtil.d(TAG, "Response => " + response);

                        if (!response.isSuccessful()) {
                            LogUtil.d(TAG, "Response => " + response.message);
                            throw new BusinessException(response.code, response.message);
                        }
                        return response;
                    }
                })
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }


    public void delBankCard(String token, Observer<BaseResponse> subscriber) {

        RequestBody requestBody = new FormBody.Builder().add("token", token)
                .build();

        mRestApi.delBankCard(requestBody)
                .map(new Function<BaseResponse, BaseResponse>() {
                    @Override
                    public BaseResponse apply(BaseResponse response) throws Exception {
                        LogUtil.d(TAG, "Response => " + response);

                        if (!response.isSuccessful()) {
                            LogUtil.d(TAG, "Response => " + response.message);
                            throw new BusinessException(response.code, response.message);
                        }
                        return response;
                    }
                })
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }


    public void addBankCard(String token, String bank_holder, String bank_name, String bank_num, String bank_type, Observer<BaseResponse> subscriber) {

        RequestBody requestBody = new FormBody.Builder().add("token", token)
                .add("user.bank_holder", bank_holder)
                .add("user.bank_name", bank_name)
                .add("user.bank_num", bank_num)
                .add("user.bank_type", bank_type)
                .build();

        mRestApi.addBankCard(requestBody)
                .map(new Function<BaseResponse, BaseResponse>() {
                    @Override
                    public BaseResponse apply(BaseResponse response) throws Exception {
                        LogUtil.d(TAG, "Response => " + response);

                        if (!response.isSuccessful()) {
                            LogUtil.d(TAG, "Response => " + response.message);
                            throw new BusinessException(response.code, response.message);
                        }
                        return response;
                    }
                })
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }


    public void updateBankCard(String token, String bank_holder, String bank_name, String bank_num, String bank_type, Observer<BaseResponse> subscriber) {

        RequestBody requestBody = new FormBody.Builder().add("token", token)
                .add("user.bank_holder", bank_holder)
                .add("user.bank_name", bank_name)
                .add("user.bank_num", bank_num)
                .add("user.bank_type", bank_type)
                .build();

        mRestApi.updateBankCard(requestBody)
                .map(new Function<BaseResponse, BaseResponse>() {
                    @Override
                    public BaseResponse apply(BaseResponse response) throws Exception {
                        LogUtil.d(TAG, "Response => " + response);

                        if (!response.isSuccessful()) {
                            LogUtil.d(TAG, "Response => " + response.message);
                            throw new BusinessException(response.code, response.message);
                        }
                        return response;
                    }
                })
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }


    public void getBank(String token, Observer<BankResponse> subscriber) {

        RequestBody requestBody = new FormBody.Builder().add("token", token)
                .build();

        mRestApi.getBank(requestBody)
                .map(new Function<BankResponse, BankResponse>() {
                    @Override
                    public BankResponse apply(BankResponse response) throws Exception {
                        LogUtil.d(TAG, "Response => " + response);

                        if (!response.isSuccessful()) {
                            LogUtil.d(TAG, "Response => " + response.message);
                            throw new BusinessException(response.code, response.message);
                        }
                        return response;
                    }
                })
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }


    public void getWXToken(String appid, String secret, String code, Observer<WXTokenResponse> subscriber) {

        mRestApi.getWXToken(appid, secret, code, "authorization_code")
                .map(new Function<WXTokenResponse, WXTokenResponse>() {
                    @Override
                    public WXTokenResponse apply(WXTokenResponse response) throws Exception {
                        LogUtil.d(TAG, "Response => " + response);
//                        if (!response.isSuccessful()) {
//                            throw new BusinessException(response.code, response.message);
//                        }
                        return response;
                    }
                })
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);

    }


    public void wxLogin(String wx_token, String wx_app_openid, Observer<LoginResponse> subscriber) {

        RequestBody requestBody = new FormBody.Builder().add("user.wx_token", wx_token)
                .add("user.wx_app_openid", wx_app_openid)
                .add("user.client_type", "1")
                .build();

        mRestApi.wxLogin(requestBody)
                .map(new Function<LoginResponse, LoginResponse>() {
                    @Override
                    public LoginResponse apply(LoginResponse response) throws Exception {
                        LogUtil.d(TAG, "Response => " + response);

                        if (!response.isSuccessful()) {
                            LogUtil.d(TAG, "Response => " + response.message);
                            throw new BusinessException(response.code, response.message);
                        }
                        return response;
                    }
                })
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }


    public void getWXInfo(String access_token, String openid, Observer<WXTokenResponse> subscriber) {

        mRestApi.getWXInfo(access_token, openid)
                .map(new Function<WXTokenResponse, WXTokenResponse>() {
                    @Override
                    public WXTokenResponse apply(WXTokenResponse response) throws Exception {
                        LogUtil.d(TAG, "Response => " + response);
//                        if (!response.isSuccessful()) {
//                            throw new BusinessException(response.code, response.message);
//                        }
                        return response;
                    }
                })
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);

    }

    public void getVIPBalance(String token, Observer<VIPBalanceResponse> subscriber) {


        RequestBody requestBody = new FormBody.Builder()
                .add("token", token)
                .build();

        mRestApi.getVipBalance(requestBody)
                .map(new Function<VIPBalanceResponse, VIPBalanceResponse>() {
                    @Override
                    public VIPBalanceResponse apply(VIPBalanceResponse response) throws Exception {
                        LogUtil.d(TAG, "Response => " + response);

                        if (!response.isSuccessful()) {
                            LogUtil.d(TAG, "Response => " + response.message);
                            throw new BusinessException(response.code, response.message);
                        }
                        return response;
                    }
                })
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }

    public void getHTML(String token, Observer<BaseResponse> subscriber) {
        RequestBody requestBody = new FormBody.Builder()
                .build();
        mRestApi.getHTML(requestBody).map(new Function<BaseResponse, BaseResponse>() {
            @Override
            public BaseResponse apply(BaseResponse response) throws Exception {
                LogUtil.d(TAG, "Response => " + response);

                if (!response.isSuccessful()) {
                    LogUtil.d(TAG, "Response => " + response.message);
                    throw new BusinessException(response.code, response.message);
                }
                return response;
            }
        }).subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }

    public void getSonMember(String token, String page, String pagesize, Observer<MemberResponse> subscriber) {
        RequestBody requestBody = new FormBody.Builder()
                .add("token", token)
                .add("page", page)
                .add("pagesize", pagesize)
                .build();
        mRestApi.getSonMember(requestBody).map(new Function<MemberResponse, MemberResponse>() {
            @Override
            public MemberResponse apply(MemberResponse response) throws Exception {
                LogUtil.d(TAG, "Response => " + response);

                if (!response.isSuccessful()) {
                    LogUtil.d(TAG, "Response => " + response.message);
                    throw new BusinessException(response.code, response.message);
                }
                return response;
            }
        }).subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }

    public void getGrandSonMember(String token, String page, String pagesize, Observer<MemberResponse> subscriber) {
        RequestBody requestBody = new FormBody.Builder()
                .add("token", token)
                .add("page", page)
                .add("pagesize", pagesize)
                .build();
        mRestApi.getGrandson(requestBody).map(new Function<MemberResponse, MemberResponse>() {
            @Override
            public MemberResponse apply(MemberResponse response) throws Exception {
                LogUtil.d(TAG, "Response => " + response);

                if (!response.isSuccessful()) {
                    LogUtil.d(TAG, "Response => " + response.message);
                    throw new BusinessException(response.code, response.message);
                }
                return response;
            }
        }).subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }


    public void getExpressPrice(Observer<ExpressPriceResponse> subscriber) {
        RequestBody requestBody = new FormBody.Builder()
                .build();
        mRestApi.getExpressPrice(requestBody).map(new Function<ExpressPriceResponse, ExpressPriceResponse>() {
            @Override
            public ExpressPriceResponse apply(ExpressPriceResponse response) throws Exception {
                LogUtil.d(TAG, "Response => " + response);

                if (!response.isSuccessful()) {
                    LogUtil.d(TAG, "Response => " + response.message);
                    throw new BusinessException(response.code, response.message);
                }
                return response;
            }
        }).subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }


    public void getAliPayUrl(Observer<ExpressPriceResponse> subscriber) {
        RequestBody requestBody = new FormBody.Builder()
                .build();
        mRestApi.getAliPayUrl(requestBody).map(new Function<ExpressPriceResponse, ExpressPriceResponse>() {
            @Override
            public ExpressPriceResponse apply(ExpressPriceResponse response) throws Exception {
                LogUtil.d(TAG, "Response => " + response);

                if (!response.isSuccessful()) {
                    LogUtil.d(TAG, "Response => " + response.message);
                    throw new BusinessException(response.code, response.message);
                }
                return response;
            }
        }).subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }


    public void getWeChatUrl(Observer<ExpressPriceResponse> subscriber) {
        RequestBody requestBody = new FormBody.Builder()
                .build();
        mRestApi.getWeChatUrl(requestBody).map(new Function<ExpressPriceResponse, ExpressPriceResponse>() {
            @Override
            public ExpressPriceResponse apply(ExpressPriceResponse response) throws Exception {
                LogUtil.d(TAG, "Response => " + response);

                if (!response.isSuccessful()) {
                    LogUtil.d(TAG, "Response => " + response.message);
                    throw new BusinessException(response.code, response.message);
                }
                return response;
            }
        }).subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }


    public void cancelOrder(String token, String order_id, Observer<BaseResponse> subscriber) {
        RequestBody requestBody = new FormBody.Builder()
                .add("token", token)
                .add("orderTab.order_id", order_id)
                .build();
        mRestApi.cancelOrder(requestBody).map(new Function<BaseResponse, BaseResponse>() {
            @Override
            public BaseResponse apply(BaseResponse response) throws Exception {
                LogUtil.d(TAG, "Response => " + response);

                if (!response.isSuccessful()) {
                    LogUtil.d(TAG, "Response => " + response.message);
                    throw new BusinessException(response.code, response.message);
                }
                return response;
            }
        }).subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }


    public void getShouyiDetail(String token, String user_id, String date, String page, String pagesize, Observer<ShouyiResponse> subscriber) {
        RequestBody requestBody = new FormBody.Builder()
                .add("page", page)
                .add("pagesize", pagesize)
                .add("feeSplittingLogTab.add_time", date)
                .add("token", token)
                .add("feeSplittingLogTab.user_id", user_id)
                .build();
        mRestApi.getShouyiDetail(requestBody).map(new Function<ShouyiResponse, ShouyiResponse>() {
            @Override
            public ShouyiResponse apply(ShouyiResponse response) throws Exception {
                LogUtil.d(TAG, "Response => " + response);

                if (!response.isSuccessful()) {
                    LogUtil.d(TAG, "Response => " + response.message);
                    throw new BusinessException(response.code, response.message);
                }
                return response;
            }
        }).subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }


    public void getExpressDetail(String token, String mail_no, Observer<ExpressResponse> subscriber) {

        RequestBody requestBody = new FormBody.Builder().add("token", token)
                .add("logisticsNodeInformationTab.mail_no", mail_no)
                .build();

        mRestApi.getExpressDetail(requestBody)
                .map(new Function<ExpressResponse, ExpressResponse>() {
                    @Override
                    public ExpressResponse apply(ExpressResponse response) throws Exception {
                        LogUtil.d(TAG, "Response => " + response);

                        if (!response.isSuccessful()) {
                            LogUtil.d(TAG, "Response => " + response.message);
                            throw new BusinessException(response.code, response.message);
                        }
                        return response;
                    }
                })
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }


    public void getKeFu(String token, Observer<KeFuResponse> subscriber) {

        RequestBody requestBody = new FormBody.Builder().add("token", token)
                .build();

        mRestApi.getKeFu(requestBody)
                .map(new Function<KeFuResponse, KeFuResponse>() {
                    @Override
                    public KeFuResponse apply(KeFuResponse response) throws Exception {
                        LogUtil.d(TAG, "Response => " + response);

                        if (!response.isSuccessful()) {
                            LogUtil.d(TAG, "Response => " + response.message);
                            throw new BusinessException(response.code, response.message);
                        }
                        return response;
                    }
                })
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }

    public void getConfigList(String token, String common_configuration_type, Observer<ConfigResponse> subscriber) {

        RequestBody requestBody = new FormBody.Builder().add("token", token)
                .add("commonConfigurationTab.common_configuration_type", common_configuration_type)
                .build();

        mRestApi.getConfigList(requestBody)
                .map(new Function<ConfigResponse, ConfigResponse>() {
                    @Override
                    public ConfigResponse apply(ConfigResponse response) throws Exception {
                        LogUtil.d(TAG, "Response => " + response);

                        if (!response.isSuccessful()) {
                            LogUtil.d(TAG, "Response => " + response.message);
                            throw new BusinessException(response.code, response.message);
                        }
                        return response;
                    }
                })
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }


    public void getGoodsTab(String token, Observer<GoodsTabResponse> subscriber) {

        RequestBody requestBody = new FormBody.Builder().add("token", token)
                .build();

        mRestApi.getGoodsTab(requestBody)
                .map(new Function<GoodsTabResponse, GoodsTabResponse>() {
                    @Override
                    public GoodsTabResponse apply(GoodsTabResponse response) throws Exception {
                        LogUtil.d(TAG, "Response => " + response);

                        if (!response.isSuccessful()) {
                            LogUtil.d(TAG, "Response => " + response.message);
                            throw new BusinessException(response.code, response.message);
                        }
                        return response;
                    }
                })
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);
    }


    public void aliply(String token, String order_id, String grade_id,String deduct, Observer<BaseResponse> subscriber) {

        FormBody.Builder builder = new FormBody.Builder().add("token", token).add("deduct",deduct);
        if (!TextUtils.isEmpty(order_id)) {
            builder.add("order_id", order_id);
        }
        if (!TextUtils.isEmpty(grade_id)) {
            builder.add("grade_id", grade_id);
        }

        mRestApi.getAlipay(builder.build())
                .map(new Function<BaseResponse, BaseResponse>() {
                    @Override


                    public BaseResponse apply(BaseResponse response) throws Exception {
                        LogUtil.d(TAG, "Response => " + response);

                        if (!response.isSuccessful()) {
                            LogUtil.d(TAG, "Response => " + response.message);
                            throw new BusinessException(response.code, response.message);

                        }
                        return response;

                    }

                })
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);

    }

    public void wechatPay(String token, String order_id, String grade_id,String deduct, Observer<WeChatPayResponse> subscriber) {

        FormBody.Builder builder = new FormBody.Builder().add("token", token).add("deduct",deduct);
        if (!TextUtils.isEmpty(order_id)) {
            builder.add("order_id", order_id);
        }
        if (!TextUtils.isEmpty(grade_id)) {
            builder.add("grade_id", grade_id);
        }

        mRestApi.getWechatPay(builder.build())
                .map(new Function<WeChatPayResponse, WeChatPayResponse>() {
                    @Override


                    public WeChatPayResponse apply(WeChatPayResponse response) throws Exception {
                        LogUtil.d(TAG, "Response => " + response);

                        if (!response.isSuccessful()) {
                            LogUtil.d(TAG, "Response => " + response.message);
                            throw new BusinessException(response.code, response.message);

                        }
                        return response;

                    }

                })
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);

    }


    public void getVip(String token, Observer<VipGradeResponse> subscriber) {

        RequestBody requestBody = new FormBody.Builder().add("token", token)
                .build();

        mRestApi.getVip(requestBody)
                .map(new Function<VipGradeResponse, VipGradeResponse>() {
                    @Override


                    public VipGradeResponse apply(VipGradeResponse response) throws Exception {
                        LogUtil.d(TAG, "Response => " + response);

                        if (!response.isSuccessful()) {
                            LogUtil.d(TAG, "Response => " + response.message);
                            throw new BusinessException(response.code, response.message);

                        }
                        return response;

                    }

                })
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(subscriber);

    }
}
